﻿Imports System.Data.Common

Partial Public Class Main

    Private Sub CalculateCounter(ByVal Countername As String,
                             ByVal CounterObject As String,
                             ByVal txtminValue As TextBox,
                             ByVal txtmaxValue As TextBox,
                             ByVal txtavgValue As TextBox,
                             ByVal SparkLine As Windows.Forms.DataVisualization.Charting.Chart)

        Try
            Dim Queryagg = Aggregate rows In PerflogCT.CounterDatas _
                         Where rows.CounterDetail.CounterName.Equals(Countername) _
                         And rows.CounterDetail.ObjectName.Equals(CounterObject) _
                         And rows.CounterDetail.MachineName.Equals(machineName) _
                         And rows.DisplayToID.DisplayString.Equals(runName) _
                         Into lowValue = Min(rows.CounterValue), maxValue = Max(rows.CounterValue), avgValue = Average(rows.CounterValue)

            txtminValue.Text = Queryagg.lowValue.ToString("#.##;-#.##;0")
            txtmaxValue.Text = Queryagg.maxValue.ToString("#.##;-#.##;0")
            txtavgValue.Text = Queryagg.avgValue.ToString("#.##;-#.##;0")

        Catch ex As Exception

        End Try

        Try
            Dim QueryGraph = From rows In PerflogCT.CounterDatas _
                                    Where rows.CounterDetail.CounterName.Equals(Countername) _
                                    And rows.CounterDetail.ObjectName.Equals(CounterObject) _
                                    And rows.CounterDetail.MachineName.Equals(machineName) _
                                    And rows.DisplayToID.DisplayString.Equals(runName) _
                                    Select rows.CounterValue, rows.CounterDateTime

            With SparkLine
                .DataSource = QueryGraph.AsEnumerable
                With .Series(0)
                    .XValueMember = "CounterDateTime"
                    .YValueMembers = "CounterValue"
                End With
                .Invalidate()
            End With

        Catch ex As Exception


        End Try

    End Sub

    Private Sub CalculateCounter(ByVal Countername As String,
                             ByVal CounterObject As String,
                             ByVal txtminValue As TextBox,
                             ByVal txtmaxValue As TextBox,
                             ByVal txtavgValue As TextBox,
                             ByVal SparkLine As Windows.Forms.DataVisualization.Charting.Chart,
                             ByVal CounterInstance As String)

        Try
            Dim Queryagg = Aggregate rows In PerflogCT.CounterDatas _
                         Where rows.CounterDetail.CounterName.Equals(Countername) _
                         And rows.CounterDetail.ObjectName.Equals(CounterObject) _
                         And rows.CounterDetail.InstanceName.Equals(CounterInstance) _
                         And rows.CounterDetail.MachineName.Equals(machineName) _
                         And rows.DisplayToID.DisplayString.Equals(runName) _
                         Into lowValue = Min(rows.CounterValue), maxValue = Max(rows.CounterValue), avgValue = Average(rows.CounterValue)

            txtminValue.Text = Queryagg.lowValue.ToString("#.##;-#.##;0")
            txtmaxValue.Text = Queryagg.maxValue.ToString("#.##;-#.##;0")
            txtavgValue.Text = Queryagg.avgValue.ToString("#.##;-#.##;0")

        Catch ex As Exception

        End Try

        Try
            Dim QueryGraph = From rows In PerflogCT.CounterDatas _
                                    Where rows.CounterDetail.CounterName.Equals(Countername) _
                                    And rows.CounterDetail.ObjectName.Equals(CounterObject) _
                                    And rows.CounterDetail.InstanceName.Equals(CounterInstance) _
                                    And rows.CounterDetail.MachineName.Equals(machineName) _
                                    And rows.DisplayToID.DisplayString.Equals(runName) _
                                    Select rows.CounterValue, rows.CounterDateTime

            With SparkLine
                .DataSource = QueryGraph.AsEnumerable
                With .Series(0)
                    .XValueMember = "CounterDateTime"
                    .YValueMembers = "CounterValue"
                End With
                .Invalidate()
            End With

        Catch ex As Exception


        End Try

    End Sub

    Public Sub GraphOnly(ByVal Countername As String,
                             ByVal CounterObject As String,
                             ByVal Chart As Windows.Forms.DataVisualization.Charting.Chart)
        Try
            Dim QueryGraph = From rows In PerflogCT.CounterDatas _
                                    Where rows.CounterDetail.CounterName.Equals(Countername) _
                                    And rows.CounterDetail.ObjectName.Equals(CounterObject) _
                                    And rows.CounterDetail.MachineName.Equals(machineName) _
                                    And rows.DisplayToID.DisplayString.Equals(runName) _
                                    Select rows.CounterValue, rows.CounterDateTime

            With Chart
                .DataSource = QueryGraph.AsEnumerable
                With .Series(0)
                    .XValueMember = "CounterDateTime"
                    .YValueMembers = "CounterValue"
                End With
                .Invalidate()
            End With

        Catch ex As Exception


        End Try
    End Sub

    Public Sub GraphOnly(ByVal Countername As String,
                             ByVal CounterObject As String,
                             ByVal Chart As Windows.Forms.DataVisualization.Charting.Chart,
                             ByVal CounterInstance As String)
        Try
            Dim QueryGraph = From rows In PerflogCT.CounterDatas _
                                    Where rows.CounterDetail.CounterName.Equals(Countername) _
                                    And rows.CounterDetail.ObjectName.Equals(CounterObject) _
                                    And rows.CounterDetail.InstanceName.Equals(CounterInstance) _
                                    And rows.CounterDetail.MachineName.Equals(machineName) _
                                    And rows.DisplayToID.DisplayString.Equals(runName) _
                                    Select rows.CounterValue, rows.CounterDateTime

            With Chart
                .DataSource = QueryGraph.AsEnumerable
                With .Series(0)
                    .XValueMember = "CounterDateTime"
                    .YValueMembers = "CounterValue"
                End With
                .Invalidate()
            End With

        Catch ex As Exception


        End Try
    End Sub


#Region "  Thresholds  "

    Private Sub FillSelectPerformanceObject()
        Dim QueryCounters = (From nodes In Counters.<Counters>.<Counter>
                            Select Name = nodes.<CounterObject>.Value
                            Order By Name).Distinct

        Me.cmbPerformanceObject.DataSource = QueryCounters.ToList


    End Sub

    Private Sub cmbPerformanceObject_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbPerformanceObject.SelectedValueChanged
        Dim QueryCounters = From nodes In Counters.<Counters>.<Counter>
                            Where nodes.<CounterObject>.Value.Equals(Me.cmbPerformanceObject.SelectedItem.ToString)
                            Order By nodes.<CounterObject>.Value Ascending, nodes.<CounterName>.Value Ascending
                            Select New Counter(nodes.<CounterName>.Value _
                                               , Me.cmbPerformanceObject.SelectedValue.ToString _
                                               , nodes.<ParentCounterObject>.Value _
                                               , nodes.<ParentCounterName>.Value _
                                               , nodes.<ParentCounterFormula>.Value _
                                               , nodes.<CounterThresholds>.<Red>.Value _
                                               , nodes.<CounterThresholds>.<Orange>.Value _
                                               , nodes.<CounterThresholds>.<Green>.Value _
                                               , nodes.<CounterDescription>.Value)



        Dim dgFont As New Font("Microsoft Sans Serif", 8.25)

        Dim styleBold As New DataGridViewCellStyle
        With styleBold
            .Font = New Font(dgFont, FontStyle.Bold)
            .BackColor = Color.LightGray
            .WrapMode = DataGridViewTriState.True
        End With

        Dim styleRed As New DataGridViewCellStyle
        With styleRed
            .BackColor = Color.Red
            .Alignment = DataGridViewContentAlignment.TopRight
            .Font = New Font(dgFont, FontStyle.Bold)
        End With

        Dim styleOrange As New DataGridViewCellStyle
        With styleOrange
            .BackColor = Color.Orange
            .Alignment = DataGridViewContentAlignment.TopRight
            .Font = New Font(dgFont, FontStyle.Bold)
        End With

        Dim styleGreen As New DataGridViewCellStyle
        With styleGreen
            .BackColor = Color.LawnGreen
            .Alignment = DataGridViewContentAlignment.TopRight
            .Font = New Font(dgFont, FontStyle.Bold)
        End With

        Dim styleDescription As New DataGridViewCellStyle
        With styleDescription
            .Alignment = DataGridViewContentAlignment.TopLeft
            .WrapMode = DataGridViewTriState.True
        End With



        With dgThresholds
            .DataSource = QueryCounters.ToList

            .Columns(1).Visible = False

            With .Columns(0)
                .HeaderText = "Counter name"
                .Width = 200
                .DefaultCellStyle = styleBold
                .ReadOnly = True
            End With
            With .Columns(2)
                .HeaderText = "Parent counter object"
                .Width = 200
            End With
            With .Columns(3)
                .HeaderText = "Parent counter name"
                .Width = 200
            End With
            With .Columns(4)
                .HeaderText = "Parent calculation"
                .Width = 80
            End With
            With .Columns(5)
                .HeaderText = "Threshold Red"
                .Width = 80
                .DefaultCellStyle = styleRed
            End With
            With .Columns(6)
                .HeaderText = "Threshold Orange"
                .Width = 80
                .DefaultCellStyle = styleOrange
            End With
            With .Columns(7)
                .HeaderText = "Threshold Green"
                .Width = 80
                .DefaultCellStyle = styleGreen
            End With
            With .Columns(8)
                .HeaderText = "Description"
                .Width = 600
            End With
            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        End With

        'Dim column As New DataGridViewComboBoxColumn
        'With column
        '    .HeaderText = "Parent counter"
        '    .DataSource = QueryCounterObjects.AsEnumerable
        '    .ValueMember = "Value"
        '    .DisplayMember = "Display"
        '    .DataPropertyName = "fkParentCounterId"
        '    .Width = 300
        '    .MinimumWidth = 200
        'End With

        'dgThresholds.Columns.Insert(4, column)
        '' bug in code?
        'dgThresholds.Columns(12).Visible = False

        'With dgThresholds
        '    With .Columns(6)
        '        .HeaderText = "% of Parent"
        '        .Width = 50
        '    End With

        'End With


    End Sub

    Private Sub cmdSaveThresholds_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSaveThresholds.Click

        For Each row As DataGridViewRow In dgThresholds.Rows
            If row.IsNewRow Then
                'inserten

            Else
                'updaten
                For Each node As XElement In Counters.Elements.<Counter>
                    If node.<CounterName>.Value = row.Cells("Countername").Value.ToString Then
                        If CStr(row.Cells("ThresholdRed").Value) Is Nothing Then
                            node.<CounterThresholds>.<Red>.Value = String.Empty
                        Else
                            node.<CounterThresholds>.<Red>.Value = CStr(row.Cells("ThresholdRed").Value)
                        End If
                        If row.Cells("ThresholdOrange").Value Is Nothing Then
                            node.<CounterThresholds>.<Orange>.Value = String.Empty
                        Else
                            node.<CounterThresholds>.<Orange>.Value = row.Cells("ThresholdOrange").Value.ToString
                        End If
                        If row.Cells("ThresholdGreen").Value Is Nothing Then
                            node.<CounterThresholds>.<Green>.Value = String.Empty
                        Else
                            node.<CounterThresholds>.<Green>.Value = row.Cells("ThresholdGreen").Value.ToString
                        End If
                        If row.Cells("CounterDescription").Value Is Nothing Then
                            node.<CounterDescription>.Value = String.Empty
                        Else
                            node.<CounterDescription>.Value = row.Cells("CounterDescription").Value.ToString
                        End If
                    End If
                Next
            End If

            Counters.Save(".\CounterThresholds\Counters.xml")

        Next
    End Sub

    Private Sub GetThresholds(ByVal ObjectName As String, _
                              ByVal CounterName As String, _
                              ByRef txtMin As TextBox, _
                              ByRef txtMax As TextBox, _
                              ByRef txtAverage As TextBox)

        Try

            Dim QueryCounters = From nodes In Counters.<Counters>.<Counter>
                                Where nodes.<CounterObject>.Value.Equals(ObjectName) And nodes.<CounterName>.Value.Equals(CounterName)
                                Select Name = nodes.<CounterName>.Value _
                                        , ParentCounterObject = nodes.<ParentCounterObject>.Value _
                                        , ParentCounterName = nodes.<ParentCounterName>.Value _
                                        , ParentCounterFormula = nodes.<ParentCounterFormula>.Value _
                                        , ThresholdRed = nodes.<CounterThresholds>.<Red>.Value _
                                        , ThresholdOrange = nodes.<CounterThresholds>.<Orange>.Value _
                                        , ThresholdGreen = nodes.<CounterThresholds>.<Green>.Value _
                                        , Description = nodes.<CounterDescription>.Value

            For Each row In QueryCounters
                Dim Tooltip As New ToolTip
                With Tooltip
                    .SetToolTip(txtMin, row.Description)
                    .SetToolTip(txtMax, row.Description)
                    .SetToolTip(txtAverage, row.Description)
                End With

                'If counter wasn't collected, don't do the coloring of the textboxes
                If txtMin.Text <> String.Empty Then

                    If CDbl(row.ThresholdRed) > CDbl(row.ThresholdOrange) Then
                        If CDbl(txtMin.Text) > CDbl(row.ThresholdRed) Then
                            txtMin.BackColor = Color.Red
                        ElseIf CDbl(txtMin.Text) > CDbl(row.ThresholdOrange) Then
                            txtMin.BackColor = Color.Orange
                        Else : txtMin.BackColor = Color.LawnGreen
                        End If
                        If CDbl(txtMax.Text) > CDbl(row.ThresholdRed) Then
                            txtMax.BackColor = Color.Red
                        ElseIf CDbl(txtMax.Text) > CDbl(row.ThresholdOrange) Then
                            txtMin.BackColor = Color.Orange
                        Else : txtMax.BackColor = Color.LawnGreen
                        End If
                        If CDbl(txtAverage.Text) > CDbl(row.ThresholdRed) Then
                            txtAverage.BackColor = Color.Red
                        ElseIf CDbl(txtAverage.Text) > CDbl(row.ThresholdOrange) Then
                            txtAverage.BackColor = Color.Orange
                        Else : txtAverage.BackColor = Color.LawnGreen
                        End If
                    Else
                        'Tested and good
                        If Not row.ThresholdGreen = String.Empty Then
                            'txtmin
                            If CDbl(txtMin.Text) >= CDbl(row.ThresholdGreen) Then
                                txtMin.BackColor = Color.LawnGreen
                            ElseIf CDbl(txtMin.Text) < CDbl(row.ThresholdGreen) _
                                And CDbl(txtMin.Text) >= CDbl(row.ThresholdOrange) Then
                                txtMin.BackColor = Color.Orange
                            ElseIf CDbl(txtMin.Text) < CDbl(row.ThresholdRed) Then
                                txtMin.BackColor = Color.Red
                            ElseIf CDbl(txtMin.Text) >= CDbl(row.ThresholdRed) _
                                    And CDbl(txtMin.Text) < CDbl(row.ThresholdOrange) Then
                                txtMin.BackColor = Color.OrangeRed
                            End If

                            'txtMax
                            If CDbl(txtMax.Text) >= CDbl(row.ThresholdGreen) Then
                                txtMax.BackColor = Color.LawnGreen
                            ElseIf CDbl(txtMax.Text) < CDbl(row.ThresholdGreen) _
                                And CDbl(txtMax.Text) >= CDbl(row.ThresholdOrange) Then
                                txtMax.BackColor = Color.Orange
                            ElseIf CDbl(txtMax.Text) < CDbl(row.ThresholdRed) Then
                                txtMax.BackColor = Color.Red
                            ElseIf CDbl(txtMax.Text) >= CDbl(row.ThresholdRed) _
                                    And CDbl(txtMax.Text) < CDbl(row.ThresholdOrange) Then
                                txtMax.BackColor = Color.OrangeRed
                            End If

                            'txtAverage
                            If CDbl(txtAverage.Text) >= CDbl(row.ThresholdGreen) Then
                                txtAverage.BackColor = Color.LawnGreen
                            ElseIf CDbl(txtAverage.Text) < CDbl(row.ThresholdGreen) _
                                And CDbl(txtAverage.Text) >= CDbl(row.ThresholdOrange) Then
                                txtAverage.BackColor = Color.Orange
                            ElseIf CDbl(txtAverage.Text) < CDbl(row.ThresholdRed) Then
                                txtAverage.BackColor = Color.Red
                            ElseIf CDbl(txtAverage.Text) >= CDbl(row.ThresholdRed) _
                                    And CDbl(txtAverage.Text) < CDbl(row.ThresholdOrange) Then
                                txtAverage.BackColor = Color.OrangeRed
                            End If

                        Else
                            'Tested and good!
                            If CDbl(txtMin.Text) < CDbl(row.ThresholdRed) Then
                                txtMin.BackColor = Color.Red
                            ElseIf CDbl(txtMin.Text) < CDbl(row.ThresholdOrange) Then
                                txtMin.BackColor = Color.Orange
                            Else : txtMin.BackColor = Color.LawnGreen
                            End If
                            If CDbl(txtMax.Text) < CDbl(row.ThresholdRed) Then
                                txtMax.BackColor = Color.Red
                            ElseIf CDbl(txtMax.Text) < CDbl(row.ThresholdOrange) Then
                                txtMax.BackColor = Color.Orange
                            Else : txtMax.BackColor = Color.LawnGreen
                            End If
                            If CDbl(txtAverage.Text) < CDbl(row.ThresholdRed) Then
                                txtAverage.BackColor = Color.Red
                            ElseIf CDbl(txtAverage.Text) < CDbl(row.ThresholdOrange) Then
                                txtAverage.BackColor = Color.Orange
                            Else : txtAverage.BackColor = Color.LawnGreen
                            End If
                        End If
                    End If


                End If

            Next

        Catch ex As Exception


        End Try


    End Sub

#End Region

End Class
